﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.video
{
      public partial class videolist : System.Web.UI.Page
      {
            D_T_Video_Exp _DAL = new D_T_Video_Exp();
            private void SetButton(int i, Button but)
            {
                  switch (i)
                  {
                        case 1:
                              but.Attributes.Add("onclick", "return GetUpdateCheckDefault();");
                              break;
                        case 2:
                              but.Attributes.Add("onclick", "return GetDelCheckDefault();");
                              break;
                        default:
                              break;
                  }
            }
            private huaguwang.WebControls.WebUserToolBar.SelectItem[] SetText()
            {
                  huaguwang.WebControls.WebUserToolBar.SelectItem[] list = { 
                new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                FuncGetText = () => this.txtVideoName.Text.Trim(),
                FuncSetText = v => { this.txtVideoName.Text = v; },
                Name = this.txtVideoName.ID
                },                                                                     
                   new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText = () =>this.HGDownList.SelectedValue.Exp_Trim(),
                    FuncSetText = v =>{this.HGDownList.SelectedValue=v;},
                    Name =this.HGDownList.ID
                },   
                    new huaguwang.WebControls.WebUserToolBar.SelectItem(){
                    FuncGetText = () =>this.dopRealName.SelectedValue.Exp_Trim(),
                    FuncSetText = v =>{this.dopRealName.SelectedValue=v;},
                    Name =this.dopRealName.ID
                },                               
            };
                  return list;
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        WebUserToolBar1.MainInit(
                            UserConst.Exp_GetUser,
                            UserConst.Exp_GetButtonList,
                            SetButton,
                            UserConst.Exp_ToolBarErr,
                            SetText()
                         );
                        if (!IsPostBack)
                        {
                              BindDropList();
                              BindList(0);
                        }
                  });
            }
            /// <summary>
            /// 绑定类别
            /// </summary>
            private void BindDropList()
            {
                  AdminPublic.Try(() =>
                  {
                        this.HGDownList.Exp_BindClassTree();
                  });
            }
            /// <summary>
            /// 分页信息
            /// </summary>
            /// <param name="startIndex"></param>
            private void BindList(int startIndex)
            {
                  AdminPublic.Try(() =>
                  {
                        var s1 = WebUserToolBar1.MainGetQuery(this.txtVideoName.ID);
                        var s2 = WebUserToolBar1.MainGetQuery(this.HGDownList.ID);
                        var s3 = WebUserToolBar1.MainGetQuery(this.dopRealName.ID);
                        this.Pager.RecordCount = _DAL.GetPageCount(s1, s2, s3);
                        this.rpVideoList.DataSource = _DAL.GetPageList(startIndex, Pager.PageSize, s1, s2, s3);
                        this.rpVideoList.DataBind();
                  });
            }

            /// <summary>
            /// 分页控件事件
            /// </summary>
            protected void Pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        BindList((e.NewPageIndex - 1) * Pager.PageSize);
                  });
            }

            /// <summary>
            /// 增加按钮事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void WebUserToolBar1_Button0(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        var sKey = this.WebUserToolBar1.EditSKey;
                        Response.Redirect("videolist_e.aspx?" + sKey, false);
                  });
            }

            /// <summary>
            /// 修改按钮的事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void WebUserToolBar1_Button1(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        var strID = Request.Params["HiddenID"].Exp_Trim();
                        var sKey = this.WebUserToolBar1.EditSKey;
                        Response.Redirect("videolist_e.aspx?ID=" + strID + "&" + sKey, false);
                  });
            }

            /// <summary>
            /// 查询按钮事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void WebUserToolBar1_Button3(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        WebUserToolBar1.MainSetQuery();
                        BindList(0);
                  });
            }

            /// <summary>
            /// 删除按钮的事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void WebUserToolBar1_Button2(object sender, EventArgs e)
            {
                  AdminPublic.Try(() =>
                  {
                        var strID = Request.Params["HiddenID"].Exp_Trim();
                        var delID = strID.Exp_SplitToList(',', Sel => Sel.Exp_IntTryParse()).Where(Sel => Sel > 0).ToList();
                        if (delID == null || delID.Count <= 0)
                        {
                              this.Exp_PageAlter("没有找到可以删除的信息");
                              return;
                        }
                        var iRet = _DAL.UpdateFlag(delID);
                        if (iRet > 0)
                        {
                              this.Exp_PageWriteLog(Log_Type.delete, "视频资料", delID);
                              BindList((Pager.CurrentPageIndex - 1) * Pager.PageSize);
                              this.Exp_PageAlter("删除视频信息操作成功");
                        }
                        else
                        {
                              this.Exp_PageAlter("删除视频信息失败");
                              return;
                        }
                  });
            }
      }
}
